package com.jy.controller.filemanager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.common.ajax.AjaxRes;
import com.jy.common.mybatis.Page;
import com.jy.common.utils.base.Const;
import com.jy.controller.base.BaseController;
import com.jy.entity.filemanager.TSProject;
import com.jy.service.filemanager.operlog.OperLogService;
import com.jy.service.filemanager.project.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController extends BaseController<TSProject> {

	@Autowired
	private ProjectService service;
	@Autowired
	private OperLogService serviceLog;

	/**
	 * @api {post} /project/findBypage  项目分页列表查询
	 * 
	 * @apiName findBypage
	 * @apiGroup project
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  项目对象
	 */
	@RequestMapping(value = "findBypage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes index(Page<TSProject> page, TSProject o) {
		AjaxRes ar = getAjaxRes();
		try {
			Page<TSProject> result = service.findByPage(o, page);
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", result);
			ar.setSucceed(p);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /project/find  项目查询
	 * 
	 * @apiName find
	 * @apiGroup project
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  项目对象
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes find(TSProject o) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSProject> list = service.find(o);
			TSProject obj = list.get(0);
			ar.setSucceed(obj);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		return ar;
	}

	@RequestMapping(value = "checkName", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes checkName(String name) {
		AjaxRes ar = getAjaxRes();
		try {
			List<TSProject> objList = service.getProListByName(name);
			if (objList.size() > 0) {
				ar.setObj(true);
			} else {
				ar.setObj(false);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /project/add  项目新增
	 * 
	 * @apiName add
	 * @apiGroup project
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  项目对象
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(TSProject o) {
		AjaxRes ar = getAjaxRes();
		try {
			o.setId(get32UUID());
			int res = service.creatProject(o);
			if (res == 1)
				ar.setSucceedMsg(Const.SAVE_SUCCEED);
			else
				ar.setFailMsg(Const.SAVE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.SAVE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /project/update  项目跟新
	 * 
	 * @apiName update
	 * @apiGroup project
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  项目对象
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes update(TSProject o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.updateProject(o);
			/*
			 * TSLog obj = new TSLog(); obj.setId(UuidUtil.get32UUID());
			 * obj.setOperType(2); obj.setOperPage("测试");
			 * obj.setOperInfo("111111"); obj.setCreateDate(new Date());
			 * serviceLog.insert(obj);
			 */
			if (res == 1)
				ar.setSucceedMsg(Const.UPDATE_SUCCEED);
			else
				ar.setFailMsg(Const.UPDATE_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.UPDATE_FAIL);
		}
		return ar;
	}

	/**
	 * @api {post} /project/del  项目删除
	 * 
	 * @apiName del
	 * @apiGroup project
	 * @apiParam {Object} Page<TSProduct> 分页对象
	 * @apiSuccess {Object} obj 1.resMsg 2.obj
	 * @apiParam {Object} TSProduct  项目对象
	 */
	@RequestMapping(value = "del", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes del(TSProject o) {
		AjaxRes ar = getAjaxRes();
		try {
			int res = service.deleteProject(o);
			if (res == 1)
				ar.setSucceedMsg(Const.DEL_SUCCEED);
			else
				ar.setFailMsg(Const.DEL_FAIL);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DEL_FAIL);
		}
		return ar;
	}
}
